import _ from 'lodash'
export default {
  name: '',
  data () {
    return {
      loadMap: new Map(),
      moreLoadWhiteList: ['S1load', 'S1speed', 'S1speedOvr'],
      allBtn: [
        {
          label: '全部',
          value: 0,
          isCheckAll: true,
          noCheckClass: ['not-check-div', 'not-check-btn'],
          checkClass: ['check-div', 'check-btn']
        },
        {
          label: 'S负载 ',
          value: 1,
          id: 'S1load',
          color: '#FF8559',
          type: '%',
          chart: null,
          step: '',
          isCheck: false,
          noCheckClass: ['not-check-div', 'not-check-btn'],
          checkClass: ['check-div', 'check-btn']
        },
        {
          label: 'S转速',
          value: 2,
          id: 'S1speed',
          color: '#F7B72D',
          type: 'r',
          chart: null,
          step: '',
          isCheck: false,
          noCheckClass: ['not-check-div', 'not-check-btn'],
          checkClass: ['check-div', 'check-btn']
        },
        {
          label: 'S倍率',
          value: 3,
          id: 'S1speedOvr',
          color: '#43D7C5',
          type: '%',
          chart: null,
          step: 'end',
          isCheck: false,
          noCheckClass: ['not-check-div', 'not-check-btn'],
          checkClass: ['check-div', 'check-btn']
        },
        {
          label: 'F实速',
          value: 4,
          id: 'path_feedrate',
          color: '#17D77F',
          type: 'mm',
          step: '',
          chart: null,
          isCheck: false,
          noCheckClass: ['not-check-div', 'not-check-btn'],
          checkClass: ['check-div', 'check-btn']
        },
        {
          label: 'F倍率',
          value: 5,
          id: 'Fovr',
          color: '#5F77FD',
          type: '%',
          chart: null,
          step: 'end',
          isCheck: false,
          noCheckClass: ['not-check-div', 'not-check-btn'],
          checkClass: ['check-div', 'check-btn']
        },
        // {
        //   label: 'S温度',
        //   value: 6,
        //   id: 'path_feedrate',
        //   color: '#E33333',
        //   type: '℃',
        //   isCheck: false,
        //   noCheckClass: ['not-check-div', 'not-check-btn'],
        //   checkClass: ['check-div', 'check-btn']
        // },
        {
          label: '程序',
          value: 7,
          id: 'program',
          chart: null,
          isCheck: false,
          noCheckClass: ['not-check-div', 'not-check-btn'],
          checkClass: ['check-div', 'check-btn']
        }
      ],
      orderData: [ 'X1', 'X', 'Y1', 'Y', 'Z1', 'Z', 'U1', 'U', 'V1', 'V', 'W1', 'W', 'A1', 'A', 'B1', 'B', 'C1', 'C' ] // 伺服轴排列顺序依据
    }
  },
  methods: {
    // 过滤出伺服轴显示
    // return 排列好的伺服轴信息
    getActList () {
      if (!this.processMessage) {
        return []
      }
      const arr = []
      Object.keys(this.processMessage).map(v => {
        const pm = v.indexOf('act')
        if (pm > -1) {
          arr.push({
            label: v.slice(0, pm),
            value: this.processMessage[v]
          })
        }
      })
      const a = arr.sort((a, b) => {
        return this.orderData.indexOf(a.label) - this.orderData.indexOf(b.label)
      })
      return a
    },
    /**
     * 解析多个S转速 S倍率
     */
    getMoreSpeed (type) {
      if (!this.processMessage) {
        return []
      }
      const arr = []
      Object.keys(this.processMessage).map(v => {
        const isSpeed = _.startsWith(v, 'S') && _.endsWith(v, 'speed')
        const isOvr = _.startsWith(v, 'S') && _.endsWith(v, 'speedOvr')
        const isSload = _.startsWith(v, 'S') && _.endsWith(v, 'load')
        isSpeed && arr.push({
          label: this.getNewSLoad(v, 'speed'),
          value: this.processMessage[v] || 0,
          unit: 'r/min',
          type: 'S1speed'
        })
        isOvr && arr.push({
          label: this.getNewSLoad(v, 'speedOvr'),
          value: this.processMessage[v] || 0,
          unit: '%',
          type: 'S1speedOvr'
        })
        isSload && arr.push({
          label: this.getNewSLoad(v, 'load'),
          value: this.processMessage[v] || 0,
          unit: 'r/min',
          type: 'S1load'
        })
      })
      return type === 'all' ? _.uniq(arr) : _.uniq(arr).filter(v => v.type !== 'S1load')
    },
    /**
     * 多个S主轴
     */
    getMoreSload (params) {
      const id = params.id
      if (!this.moreLoadWhiteList.includes(id)) {
        this.loadMap.set(id, [{
          value: this.processMessage[id] || 0,
          label: params.label
        }])
        return this.loadMap.get(id) || []
      }
      let arr = this.getMoreSpeed('all').filter(j => j.type === id)
      this.loadMap.set(id, arr)
      return this.loadMap.get(id) || []
    },
    /**
     * @param {*} item 单个元素
     * @param {*} type 类型
     */
    getNewSLoad (item, type) {
      const member = {
        speed: '转速',
        speedOvr: '倍率',
        load: '负载'
      }
      const isNewBox = Object.keys(this.processMessage).includes('path_cur')
      let firstName = isNewBox ? item.slice(1) : item
      return `${_.replace(firstName, type, '')}${member[type]}`
    }
  }
}
